package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.*;
import com.project.entity.Class;
import com.project.myBeans.ResponseBean;
import com.project.myBeans.StudentInfoDetail;
import com.project.service.*;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/3/29.
 * <p>
 * 学生信息相关操作
 */
@Controller
@RequestMapping("/student")
public class StudentController extends BaseService {
    @Autowired
    UserService userService;
    @Autowired
    StudentInfoService studentInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private ClassService classService;
    @Autowired
    private SchoolManagerInfoService schoolManagerInfoService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MajorService majorService;



    public static final int STUDENTSTATE_NO = 1;//未实习

    public static final int STUDENTSTATE_APPLY = 2;//实习申请中（当前未在实习）

    public static final int STUDENTSTATE_ING = 3;//正在实习中

    public static final int STUDENTSTATE_CHANGE = 4;//实习变更申请中（当前仍在实习）

    public static final int STUDENTSTATE_GRADUATE = -1;//已经毕业

    /**
     * author: WheelChen
     * date: 2017-5-24
     * function: 根据userId修改学生表信息
     *
     * @param studentInfo
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean modifyOneByUserId(@RequestBody StudentInfo studentInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            processSuccessDeleteUpdate(studentInfoService.modifyByUserIdSelective(studentInfo), responseBean);
            return responseBean;
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序失败");
            return responseBean;
        }
    }

    @RequestMapping(value = "/modifyOneById", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean modifyOneById(@RequestBody StudentInfo studentInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            processSuccessDeleteUpdate(studentInfoService.modifyOneById(studentInfo), responseBean);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序失败");
            return responseBean;
        }
    }

    @RequestMapping(value = "/modifyOrAddDetail", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/7
     * function: 修改学生详细信息（影响用户表 +学生信息表）
     *
     * @param studentInfoDetail 学生详细信息
     * @return
     * @deprecated
     */
    public ResponseBean modifyOrAddDetail(@RequestBody StudentInfoDetail studentInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (studentInfoDetail.getUser() != null)
                userService.updateUser(studentInfoDetail.getUser());
            if (studentInfoDetail.getStudentInfo() != null)
                if (studentInfoService.findOneByUserId(studentInfoDetail.getStudentInfo().getUserid()) != null)//判断学生信息表记录是否被创建
                    studentInfoService.modifyByUserIdSelective(studentInfoDetail.getStudentInfo());
                else
                    studentInfoService.add(studentInfoDetail.getStudentInfo());
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/addDetailOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/28
     * function: 添加学生详细信息 （影响用户表 + 学生信息表）
     *
     * @param studentInfoDetail 学生详细信息
     * @return
     */
    public ResponseBean addDetailOne(@RequestBody StudentInfoDetail studentInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            studentInfoService.addDetailOne(studentInfoDetail);
            responseBean.setSuccess(true);
            responseBean.setMessage("添加成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/modifyDetailOne", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/28
     * function: 修改学生详细信息 （影响用户表 + 学生信息表）
     *
     * @param studentInfoDetail 学生详细信息
     * @return
     */
    public ResponseBean modifyDetailOne(@RequestBody StudentInfoDetail studentInfoDetail) {
        ResponseBean responseBean = new ResponseBean();
        try {
            studentInfoService.modifyDetailOne(studentInfoDetail);
            responseBean.setSuccess(true);
            responseBean.setMessage("修改成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    /**
     * author: WheelChen
     * date: 2017-5-15
     * function: 查询学生个人详细信息
     *
     * @param userId 学生userId
     * @return
     */
    @RequestMapping(value = "/findDetail/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseBean findDetailOneByUserId(@PathVariable("userId") Integer userId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(studentInfoService.findDetailByUserId(userId));
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }


    @RequestMapping(value = "/findDetailBatchByInternAdvisor", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/6
     * function: 根据校内指导老师userid批量查找学生详细信息 + 分页 + 条件
     *
     * @param studentInfo
     * @return
     */
    public ResponseBean findDetailBatchByInternAdvisor(@RequestBody User user) {
        ResponseBean responseBean = new ResponseBean();
        try {
            StudentInfo studentInfo = new StudentInfo();
            //先找出通过老师userid找到教师信息表id
            if (user.getId() != null) {
                TeacherInfo teacherInfo = teacherInfoService.findOneByUserId(user.getId());
                studentInfo.setPageSize(user.getPageSize());
                studentInfo.setPageIndex(user.getPageIndex() - 1);
                if (teacherInfo != null) {
                    studentInfo.setInternadvisor(teacherInfo.getId());
                    //再通过教师信息表id查符合条件学生
                    List<StudentInfo> studentInfoList = studentInfoService.findBatchByCondition(studentInfo);
                    //找出每个学生的详细信息
                    List<StudentInfoDetail> studentInfoDetails = new ArrayList<>();
                    for (StudentInfo studentInfo1 : studentInfoList) {
                        studentInfoDetails.add(studentInfoService.findDetailById(studentInfo1.getId()));
                    }
                    responseBean.setData(studentInfoDetails);
                    responseBean.setTotal(studentInfoDetails.size());
                    responseBean.setMessage("获取成功");
                    responseBean.setSuccess(true);
                } else {
                    responseBean.setMessage("缺少教师信息表记录");
                    responseBean.setSuccess(false);
                }
            } else {
                responseBean.setMessage("缺少老师userId");
                responseBean.setSuccess(false);
            }
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

//    @RequestMapping(value = "/findDetailBatchBySchoolId", method = RequestMethod.POST)
//    @ResponseBody
//    /**
//     * author: WheelChen
//     * date: 2017/7/25
//     * function: 根据学院id批量查找学生详细信息 + 分页 + 条件
//     *
//     * @param schoolManagerInfo 学院管理员信息
//     * @return
//     */
//    public ResponseBean findDetailBatchBySchoolId(@RequestBody SchoolManagerInfo schoolManagerInfo){
//        ResponseBean responseBean = new ResponseBean();
//        try {
//            //获取学院管理员的学院id
//            SchoolManagerInfo schoolManagerInfo1 = schoolManagerInfoService.findByUserId(schoolManagerInfo.getUserid());
//            Integer schoolId = schoolManagerInfo1.getSchoolid();
//            //查找对应的班级 classList
//            List<Class> classList = classService.findBatchBySchoolId(schoolId);
//            StudentInfo studentInfo = new StudentInfo();
//            studentInfo.setPageSize(schoolManagerInfo.getPageSize());
//            studentInfo.setPageIndex(schoolManagerInfo.getPageIndex() - 1);
//            studentInfo.setClassList(classList);
//            //再通过学生信息信息表id查符合条件学生
//            List<StudentInfo> studentInfoList = studentInfoService.findBatchByCondition(studentInfo);
//            //找出每个学生的详细信息
//            List<StudentInfoDetail> studentInfoDetails = new ArrayList<>();
//            for (StudentInfo studentInfo1 : studentInfoList) {
//                studentInfoDetails.add(studentInfoService.findDetailById(studentInfo1.getId()));
//            }
//            responseBean.setData(studentInfoDetails);
//            responseBean.setTotal(studentInfo.getTotal());
//            responseBean.setMessage("获取成功");
//            responseBean.setSuccess(true);
//        } catch (Exception e) {
//            responseBean.setSuccess(false);
//            responseBean.setMessage("程序异常");
//        }
//        return responseBean;
//    }

    @RequestMapping(value = "/findDetailBatchBySchoolId", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/25
     * function: 根据学院id批量查找学生详细信息 + 分页 + 条件
     *
     * @param schoolManagerInfo 学院管理员信息
     * @return
     */
    public ResponseBean findDetailBatchBySchoolId(@RequestBody SchoolManagerInfo schoolManagerInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //获取学院管理员的学院id
            SchoolManagerInfo schoolManagerInfo1 = schoolManagerInfoService.findByUserId(schoolManagerInfo.getUserid());
            Integer schoolId = schoolManagerInfo1.getSchoolid();
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setPageSize(schoolManagerInfo.getPageSize());
            studentInfo.setPageIndex(schoolManagerInfo.getPageIndex() - 1);
            studentInfo.setSchoolid(schoolId);
            //再通过学生信息信息表id查符合条件学生
            List<StudentInfo> studentInfoList = studentInfoService.findBatchByCondition(studentInfo);
            //找出每个学生的详细信息
            List<JSONObject> jsonObjectList = new ArrayList<>();
            for (StudentInfo studentInfo1 : studentInfoList) {
                JSONObject jsonObject = new JSONObject();
                //学生表信息
                jsonObject.put("studentInfo", studentInfo1);
                //用户表信息
                jsonObject.put("user", userService.findOneByUserID(studentInfo1.getUserid()));
                //系
                if (studentInfo1.getDepartmentid() != null)
                    jsonObject.put("department", departmentService.findOne(studentInfo1.getDepartmentid()));
                //专业
                if (studentInfo1.getMajorid() != null)
                    jsonObject.put("major", majorService.findOne(studentInfo1.getMajorid()));
                //班级
                if (studentInfo1.getClassid() != null)
                    jsonObject.put("class", classService.findOne(studentInfo1.getClassid()));
//                studentInfoDetails.add(studentInfoService.findDetailById(studentInfo1.getId()));
                jsonObjectList.add(jsonObject);
            }
            responseBean.setData(jsonObjectList);
            responseBean.setTotal(studentInfo.getTotal());
            responseBean.setMessage("获取成功");
            responseBean.setSuccess(true);
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }

    @RequestMapping(value = "/findBatchByInternAdvisorUserId/{internAdvisorUserId}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/4
     * function: 根据internAdvisorUserId获取该老师指导下的学生们的用户表信息（仅解密姓名）
     *
     * @param internAdvisorUserId 学校实习指导老师用户表id
     * @return
     */
    public ResponseBean findBatchByInternAdvisorUserId(@PathVariable("internAdvisorUserId") Integer internAdvisorUserId) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(studentService.findBatchByInternAdvisorUserId(internAdvisorUserId));
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
